/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package cn.ac.iie.di.wx.server.oracleservice;

import cn.ac.iie.di.wx.server.common.RuntimeEnv;
import cn.ac.iie.di.wx.server.utils.OracleConnectionUtil;
import cn.ac.iie.di.wx.server.vo.OracleUpdateVo;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;
import java.util.logging.Level;
import org.apache.log4j.Logger;

/**
 *
 * @author Administrator
 */
public class UpdateOracleThread implements Runnable {

    Logger logger = Logger.getLogger(UpdateOracleThread.class.getName());

    @Override
    public void run() {
        while (true) {
            Connection connection = null;
            PreparedStatement ps = null;
            String sql = "update t_search_work set createtime = ? where opid = ?";
            try {
                OracleUpdateVo vo = RuntimeEnv.oracleUpdateQueue.poll(1, TimeUnit.SECONDS);
                if (vo == null) {
//                    logger.info("no data");
                } else {
                    logger.info(vo.getOpid());

                    connection = OracleConnectionUtil.getConnection();
                    ps = connection.prepareStatement(sql);
                    ps.setTimestamp(1, vo.getTimeStamp());
                    ps.setLong(2, vo.getOpid());
                    int exe = ps.executeUpdate();
                    if (exe == 1) {
                        logger.info("update sucess " + exe);
                        if (vo.getType().equals("OCR")) {
                            logger.info("update OCR sucess " + exe);
                            RuntimeEnv.ORC_OCRCount.getAndIncrement();
                        }else{
                            logger.info("update PBP sucess " + exe);
                            RuntimeEnv.ORC_PBPCount.getAndIncrement();
                        }
                    }else{
                        if (vo.getType().equals("OCR")) {
                            logger.info("update OCR error " + exe);
                        }else{
                            logger.info("update PBP error " + exe);
                        }
                    }
                }
                //批量
//                connection = OracleConnectionUtil.getConnection();
//                ps = connection.prepareStatement(sql);
//                int count = 0;
//                //每次批量更新100条，
//                for (int i = 0; i < 100; i++) {
//                    OracleUpdateVo batchVo = RuntimeEnv.oracleUpdateQueue.poll(100, TimeUnit.MILLISECONDS);
//                    if (batchVo == null) {
//                        //                    logger.info("no data");
//                    } else {
//                        ps.setTimestamp(1, batchVo.getTimeStamp());
//                        ps.setLong(2, batchVo.getOpid());
//                        ps.addBatch();
//                        count++;
//                    }
//
//                }
//                if (count > 0) {
//                    int[] d = ps.executeBatch();
//                    for (int i = 0; i < d.length; i++) {
//                        if (d[i] == 1) {
//                            //更新成功
//                        }
//                    }
//                } else {
//                    logger.info("no data add to the update");
//                }

            } catch (SQLException ex) {
                logger.error("oracle connection failure");
                logger.error(ex.getMessage(), ex);
            } catch (InterruptedException ex) {
                logger.error(ex.getMessage(), ex);
            } catch (Exception ex) {
                logger.error(ex.getMessage(), ex);
            } finally {
                OracleConnectionUtil.releaseConn(connection, ps, null);
            }
        }
    }

}
